package com.study.service;

import com.study.dao.accountDao;
import com.study.util.DruidUtils;
import org.apache.commons.dbutils.DbUtils;

import java.sql.Connection;
import java.sql.SQLException;

public class AccountService {
    accountDao accountDao = new accountDao();
    public void transfer(String outUser, String inUser, Double money) {
        Connection connection = null;
        try {
            //1、获取连接对象
            connection = DruidUtils.getConnection();
            //2、开启事务
            connection.setAutoCommit(false);
            //减钱（调用dao层的方法）
            accountDao.out(connection,outUser,money);
            int i = 100/0;
            accountDao.in(connection,inUser,money);
            DbUtils.commitAndCloseQuietly(connection);
        } catch (SQLException e) {
            System.out.println("执行事务回滚操作");
            DbUtils.rollbackAndCloseQuietly(connection);
            throw new RuntimeException(e);
        }

    }
}
